package com.sunmnet.j2ee.bean.powerdb.designer.chart;

import com.sunmnet.j2ee.bean.powerdb.designer.cus.CusEntity;
import com.sunmnet.j2ee.bean.pub.enumerator.SysEnumShowValue;
import com.sunmnet.j2ee.entity.powerdb.designer.EntityRelate;
import com.sunmnet.j2ee.entity.powerdb.designer.Field;
import com.sunmnet.j2ee.entity.powerdb.designer.Project;
import com.sunmnet.j2ee.service.powerdb.designer.impl.PDMServiceImpl;
import com.sunmnet.j2ee.utils.EnumTools;
import com.sunmnet.j2ee.utils.JsonUtils;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.*;

/**
 * <PRE>
 * 前端绘图对象chart
 * </PRE>
 * <B>项    目：</B>J2EE开发平台
 * <B>技术支持：</B>三盟科技 (c) 2016
 *
 * @author 黄坚：huangjian@sunmnet.com
 * @version 1.0 2016/7/29
 * @since jdk版本：jdk1.7
 */
public class BeChart {

/*	{
		"id": 99991,
			"name": "chartA",
			"domainShapeInfo": "{\"b753da307da286a068ed71edac75045a\":{\"id\":\"b753da307da286a068ed71edac75045a\",\"position\":{\"x\":76,\"y\":328},\"shapeType\":\"ENTITY\",\"name\":\"学生\",\"description\":\"\",\"parentId\":\"60ab98a0e5418c8fa8e7c11589009c90\",\"parentName\":null,\"implementsIdSet\":[],\"implementsNameSet\":[],\"properties\":[{\"id\":\"45616713be3031e79f14de9743bc4ff1\",\"name\":\"年龄\",\"type\":\"String\",\"genericity\":null,\"relation\":null,\"nullable\":true,\"isUnique\":false,\"transientPro\":false}],\"actions\":[{\"id\":\"c286726403082a31ecfa56320a04228b\",\"name\":\"学习\",\"returnType\":\"void\",\"modifier\":\"PUBLIC\",\"arguments\":[],\"final\":false,\"static\":false,\"abstract\":false}],\"entityType\":\"ENTITY\"},\"fbdb4a638ac21c5a808a2cafb4ec210e\":{\"id\":\"fbdb4a638ac21c5a808a2cafb4ec210e\",\"position\":{\"x\":343,\"y\":95},\"shapeType\":\"ENTITY\",\"name\":\"Entity1\",\"description\":\"\",\"parentId\":null,\"parentName\":null,\"implementsIdSet\":[],\"implementsNameSet\":[],\"properties\":[],\"actions\":[],\"entityType\":\"ENTITY\"},\"60ab98a0e5418c8fa8e7c11589009c90\":{\"id\":\"60ab98a0e5418c8fa8e7c11589009c90\",\"position\":{\"x\":257,\"y\":61},\"shapeType\":\"ENTITY\",\"name\":\"Entity2\",\"description\":\"\",\"parentId\":null,\"parentName\":null,\"implementsIdSet\":[],\"implementsNameSet\":[],\"properties\":[],\"actions\":[],\"entityType\":\"ENTITY\"}}",
			"lineInfo": "{\"7b956a5271d5430466ae00d9f72f3cba\":{\"id\":\"7b956a5271d5430466ae00d9f72f3cba\",\"fromShapeId\":\"b753da307da286a068ed71edac75045a\",\"toShapeId\":\"60ab98a0e5418c8fa8e7c11589009c90\",\"relationType\":\"extends\",\"description\":null,\"lineType\":\"line_of_centers\",\"points\":\"153.08228980322002,328 289.123472286947,119 \"}}"
	},*/

	private String id;

	private String name;

	private BeProject project;

	/**
	 * JSON String
	 * {\"b753da307da286a068ed71edac75045a\":{\"id\":\"b753da307da286a068ed71edac75045a\",
	 * \"position\":{\"x\":76,\"y\":328},\"shapeType\":\"ENTITY\",\"name\":\"学生\",\
	 * "description\":\"\",\"parentId\":\"60ab98a0e5418c8fa8e7c11589009c90\",
	 * \"parentName\":null,\"implementsIdSet\":[],\"implementsNameSet\":[],
	 * \"properties\":[{\"id\":\"45616713be3031e79f14de9743bc4ff1\",
	 * \"name\":\"年龄\",\"type\":\"String\",\"genericity\":null,
	 * \"relation\":null,\"nullable\":true,\"isUnique\":false,
	 * \"transientPro\":false}],\"actions\":
	 * [{\"id\":\"c286726403082a31ecfa56320a04228b\",\"name\":\"学习\",\"returnType\":\"void\",\"modifier\":
	 * \"PUBLIC\",\"arguments\":[],\"final\":false,\"static\":false,\"abstract\":false}],\"entityType\":\"ENTITY\"},
	 * \"fbdb4a638ac21c5a808a2cafb4ec210e\":{\"id\":\"fbdb4a638ac21c5a808a2cafb4ec210e\",
	 * \"position\":{\"x\":343,\"y\":95},\"shapeType\":\"ENTITY\",\"name\":\"Entity1\",\"description\":\"\",
	 * \"parentId\":null,\"parentName\":null,\"implementsIdSet\":[],\"implementsNameSet\":[],\"properties\":[],
	 * \"actions\":[],\"entityType\":\"ENTITY\"},\"60ab98a0e5418c8fa8e7c11589009c90\":{\"id\":
	 * \"60ab98a0e5418c8fa8e7c11589009c90\",\"position\":{\"x\":257,\"y\":61},\"shapeType\":
	 * \"ENTITY\",\"name\":\"Entity2\",\"description\":\"\",\"parentId\":null,\"parentName\":null,\
	 * "implementsIdSet\":[],\"implementsNameSet\":[],\"properties\":[],\"actions\":[],\"entityType\":\"ENTITY\"}}
	 */
	private String domainShapeInfo;

	/**
	 * JSON String
	 * {\"7b956a5271d5430466ae00d9f72f3cba\":{\"id\":\"7b956a5271d5430466ae00d9f72f3cba\",\"fromShapeId\":\"b753da307da286a068ed71edac75045a\",\"toShapeId\":\"60ab98a0e5418c8fa8e7c11589009c90\",\"relationType\":\"extends\",\"description\":null,\"lineType\":\"line_of_centers\",\"points\":\"153.08228980322002,328 289.123472286947,119 \"}}
	 */
	private String lineInfo;


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDomainShapeInfo() {
		return domainShapeInfo;
	}

	public void setDomainShapeInfo(String domainShapeInfo) {
		this.domainShapeInfo = domainShapeInfo;
	}

	public String getLineInfo() {
		return lineInfo;
	}

	public void setLineInfo(String lineInfo) {
		this.lineInfo = lineInfo;
	}

	@Override
	public String toString() {
		return new ReflectionToStringBuilder(
				this,
				ToStringStyle.MULTI_LINE_STYLE)
				.toString();
	}

	public BeProject getProject() {
		return project;
	}

	public void setProject(BeProject project) {
		this.project = project;
	}


	/* **********************************
		自定义PDM对象和前端使用的chart对象转换 by huangjian
	* ***************************************************** */
	public void setProject(Project project) {
		this.project = new BeProject(project.getId(), project.getName());
	}

	public void setLineInfo(List<EntityRelate> relates) {
		Map<String, LineInfo> map = new HashMap<>();
		LineInfo line = null;
		for (EntityRelate rel : relates) {
			line = getLine(rel);
			map.put(line.getId(), line);
		}
		this.lineInfo = JsonUtils.toJson(map);
	}

	private LineInfo getLine(EntityRelate rel) {
		LineInfo line = new LineInfo();
		Multiplicity muc = null;
		BePropertiesRel start = null;
		BePropertiesRel end = null;
		BePosition pos = null;
		line.setId(rel.getId() + "");
		line.setName(rel.getName());
		line.setFromShapeId(rel.getStartObjectId() + "");
		line.setToShapeId(rel.getEndObjectId() + "");
		line.setLineType("line_of_centers");
		line.setRelationType("associate");
		line.setPoints(rel.getPoints());
		String mulJson = rel.getMultiplicityJson();
		if (StringUtils.isNotEmpty(mulJson)) {
			muc = JsonUtils.fromJsonObject(mulJson
							.replace(PDMServiceImpl.SYNBOL, "\""),
					Multiplicity.class);
		} else {

			muc = new Multiplicity(
					new BePropertiesRel("1", "df1", "1", new BePosition(100, 200)),
					new BePropertiesRel("2", "df2", "1", new BePosition(200, 200)));
		}
		line.setMultiplicity(muc);
		return line;
	}

	public void setDomainShapeInfo(List<CusEntity> entitys) {
		Map<String, DomainShapeInfo> map = new HashMap<>();
		DomainShapeInfo shape = null;
		for (CusEntity entity : entitys) {
			shape = getShape(entity);
			map.put(entity.getId() + "", shape);
		}
		this.domainShapeInfo = JsonUtils.toJson(map);
	}

	private DomainShapeInfo getShape(CusEntity en) {
		DomainShapeInfo sh = new DomainShapeInfo();
		sh.setId(en.getId() + "");
		sh.setName(en.getNameCn());
		sh.setEntityType("ENTITY");
		sh.setShapeType("ENTITY");
		sh.setPosition(new BePosition(en.getxPosition(),
				en.getyPosition()));
		sh.setProperties(getProperties(en.getFields()));
		return sh;
	}

	private List<BeProperties> getProperties(Iterable<Field> fields) {
		List<BeProperties> pros = new ArrayList<>();
		BeProperties pro = null;
		Iterator<Field> it = fields.iterator();
		while (it.hasNext()) {
			pro = getPropertie(it.next());
			pros.add(pro);
		}
		return pros;
	}

	private BeProperties getPropertie(Field field) {
		BeProperties pro = new BeProperties();
		pro.setId(field.getId() + "");
		pro.setType(enumType(field.getType()));
		pro.setName(field.getNameCn());
		pro.setNullable(field.getIsNull() == 1);
		pro.setGenericity(null);
		pro.setRelation(null);
		pro.setTransientPro(false);
		pro.setUnique(field.getIsPmKey() == 1);
		return pro;
	}

	private static List<SysEnumShowValue> fieldTypeEnums = EnumTools.getInstance().findActiveEnumShowValue("pdm_field", "type");

	private String enumType(Short type) {
		String val = "";

		for (SysEnumShowValue fieldType : fieldTypeEnums) {
			if (type.equals(fieldType.getEnumCode())) {
				val = fieldType.getEnumValue();
				break;
			}
		}

		return val;
	}

	public static void main(String[] args) {
		String json = "{\n" +
				"                \"mapping\": \"1\", \n" +
				"                \"name\": \"property1\", \n" +
				"                \"position\": {\n" +
				"                    \"top\": 116.46875, \n" +
				"                    \"left\": 301.78125\n" +
				"                }";
//		Multiplicity mul = JsonUtils.fromJsonObject(json, Multiplicity.class);
//		System.out.println(mul);

		BePosition pos1 = new BePosition(84, 352);
		BePosition pos2 = new BePosition(84, 352);
		BePropertiesRel start = new BePropertiesRel("1", "f1", "1", pos1);
		BePropertiesRel end = new BePropertiesRel("2", "f2", "1", pos2);
		Multiplicity muc = new Multiplicity(start, end);
		json = JsonUtils.toJson(muc);
		System.out.println(json);
		muc = JsonUtils.fromJsonObject(json, Multiplicity.class);
		System.out.println(muc);


	}
}